﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using GalaSoft.MvvmLight;

namespace RIFT.App.ViewModel
{
    using Assets;
    using Model;
    using System.Windows.Input;
    using GalaSoft.MvvmLight.Command;
    using System.Collections.ObjectModel;

    public class MainWindowViewModel : ViewModelBase
    {
        private readonly RelayCommand<IPackageItem> selectedFileChangedCommand;
        private readonly RelayCommand openFileCommand;
        private readonly RelayCommand openFolderCommand;
        private readonly RelayCommand exportCommand;
        private readonly RelayCommand exportAllCommand;
        private readonly RelayCommand<RecentFile> openRecentFileCommand;
        private readonly RelayCommand exitCommand;
        private readonly RelayCommand aboutCommand;

        private ObservableCollection<RiftPackageItem> openPackages;
        private RiftPackageFileItem selectedFile;
        private RiftPackageItem selectedPackage;

        public MainWindowViewModel()
        {
            this.selectedFileChangedCommand =
                new RelayCommand<IPackageItem>(SelectedFileChangedExecute);

            this.openFileCommand = new RelayCommand(OpenFileExecute);
            this.openFolderCommand = new RelayCommand(OpenFolderExecute);
            this.exportCommand = new RelayCommand(ExportExecute);
            this.exportAllCommand = new RelayCommand(ExportAllExecute);
            this.openRecentFileCommand = new RelayCommand<RecentFile>(OpenRecentFileExecute);
            this.exitCommand = new RelayCommand(App.Current.Shutdown);
            this.aboutCommand = new RelayCommand(AboutExecute);

            this.openPackages = new ObservableCollection<RiftPackageItem>();
        }

        public ObservableCollection<RiftPackageItem> OpenPackages
        {
            get
            {
                return openPackages;
            }
            set
            {
                if (openPackages != value)
                {
                    openPackages = value;
                    RaisePropertyChanged("OpenPackages");
                }
            }
        }

        public RiftPackageFileItem SelectedFile
        {
            get
            {
                return selectedFile;
            }
            set
            {
                if (selectedFile != value)
                {
                    selectedFile = value;
                    RaisePropertyChanged("SelectedFile");
                }
            }
        }

        public RiftPackageItem SelectedPackage
        {
            get
            {
                return selectedPackage;
            }
            set
            {
                if (selectedPackage != value)
                {
                    selectedPackage = value;
                    RaisePropertyChanged("SelectedPackage");
                }
            }
        }

        public ICommand SelectedFileChanged
        {
            get
            {
                return selectedFileChangedCommand;
            }
        }

        public ICommand OpenFile
        {
            get
            {
                return openFileCommand;
            }
        }

        public ICommand OpenFolder
        {
            get
            {
                return openFolderCommand;
            }
        }

        public ICommand Export
        {
            get
            {
                return exportCommand;
            }
        }

        public ICommand ExportAll
        {
            get
            {
                return exportAllCommand;
            }
        }

        public ICommand OpenRecentFile
        {
            get
            {
                return openRecentFileCommand;
            }
        }

        public ICommand Exit
        {
            get
            {
                return exitCommand;
            }
        }

        public ICommand About
        {
            get
            {
                return aboutCommand;
            }
        }

        private void SelectedFileChangedExecute(IPackageItem file)
        {
            if (file is RiftPackageFileItem)
            {
                this.SelectedFile = (RiftPackageFileItem)file;
            }
            else if (file is RiftPackageItem)
            {
                this.selectedPackage = (RiftPackageItem)file;
            }
        }

        private void OpenFileExecute()
        {
            var openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "RIFT package|*.pak|All Files|*.*";
            openFileDialog.Multiselect = true;
            openFileDialog.FileOk += (s, e) =>
            {
                foreach (var fileName in openFileDialog.FileNames)
                {
                    OpenPackage(fileName);
                }
            };

            openFileDialog.ShowDialog();
        }

        private void OpenFolderExecute()
        {
            var openFolderDialog = new FolderBrowserDialog();
            if (openFolderDialog.ShowDialog() == DialogResult.OK)
            {
                var files = Directory.GetFiles(openFolderDialog.SelectedPath,
                    "*.pak", SearchOption.AllDirectories);

                foreach (var fileName in files)
                {
                    OpenPackage(fileName);
                }
            }
        }

        private void ExportExecute()
        {
        }

        private void ExportAllExecute()
        {
        }

        private void OpenRecentFileExecute(RecentFile file)
        {
        }

        private void AboutExecute()
        {
        }

        private void OpenPackage(string path)
        {
            var headers = RiftPackage.GetHeaders(path);
            var packageName = Path.GetFileName(path);

            var packageItem = new RiftPackageItem()
            {
                FileName = packageName
            };

            int i = 1;
            foreach (var header in headers)
            {
                packageItem.Files.Add(new RiftPackageFileItem()
                {
                    FileName = string.Format("Image{0:0000}.dds", i),
                    FileType = "Image",
                    Header = header
                });

                i++;
            }

            OpenPackages.Add(packageItem);
        }
    }
}
